package JasPerl::TagExt::BodyContent;

use strict;
use warnings;

use parent qw(JasPerl::JspWriter);

use JasPerl::Util::Exceptions;

sub new {
    my $class = shift;
    bless [ ], $class;
}

sub get_buffer_size {
    return JasPerl::JspWriter::UNBOUNDED_BUFFER;
}

sub get_remaining {
    return JasPerl::JspWriter::UNBOUNDED_BUFFER;
}

sub is_auto_flush {
    # BodyContent object cannot be in auto-flush mode
}

sub clear {
    @{$_[0]} = ( );
}

sub flush {
    JasPerl::Util::IOException->throw("cannot flush $_[0]");
}

sub print {
    push @{+shift}, map { defined $_ ? $_ : 'null' } @_;
}

sub write {
    my ($self, $buf, $len, $off) = @_;
    JasPerl::Util::NullPointerException->throw()
        unless defined $buf;
    $buf = substr($buf, $off || 0, $len)
        if defined $len;
    push @{$self}, $buf;
    return length $buf;
}

sub trim {
    my $self = shift;
    my $buffer = $self->as_string;
    # java.lang.String.trim removes characters <= \u0020
    $buffer =~ s/^[\x{00}-\x{20}]+//;
    $buffer =~ s/[\x{00}-\x{20}]+$//;
    return $buffer;
}

sub as_string {
    return join('', @{$_[0]});
}

1;
